package com.lx.rpc.framework.consumerproxy;

import com.lx.rpc.framework.base.Invocation;
import com.lx.rpc.framework.protocol.Protocol;
import com.lx.rpc.framework.protocol.URL;
import com.lx.rpc.framework.protocol.factory.ProtocolFactory;
import com.lx.rpc.framework.register.LocalRegister;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 消费者代理工厂
 * @author wcy
 */
public class ProxyFactory<T> {


     public static <T> T getProxy( final  Class interfaceClass){


         return (T)Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class[]{interfaceClass}, new InvocationHandler() {

             //代理逻辑
             @Override
             public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

                 //接口名称
                 String interfaceName = interfaceClass.getName();
                 //方法名称
                 String methodName = method.getName();
                 //参数类型列表
                 Class<?>[] parameterTypes = method.getParameterTypes();


                 Protocol protocol = ProtocolFactory.getProtocol();



                 Invocation invocation = new Invocation(interfaceName, methodName, parameterTypes, args);

                 String result = protocol.send(new URL("localhost", 8080), invocation);


                 return result;
             }
         });



     }


}
